package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查找业务大类
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByTypeSn("business");
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
        return "messageBoard/list";
    }




    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(MessageBoard messageBoard){
        if (messageBoard.getId() != null) {
            messageBoardService.update(messageBoard);
        }else {
            messageBoardService.save(messageBoard);
        }
        return new JsonResult(true);
    }

    @RequestMapping("/messageDetails")
    public String messageDetails(Model model,Long id){
        MessageBoard message = messageBoardService.selectReplyByMessageId(id);
        model.addAttribute("message",message);
        return "/messageBoard/messageDetails";
    }
}
